package com.unohacha.qqj.fzdr.main;


import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.adapter.MyImageAdapter;
import com.unohacha.qqj.fzdr.bean.find.FindListBean;
import com.unohacha.qqj.fzdr.bean.homebean.HomeSellerAreaListBean;
import com.unohacha.qqj.fzdr.bean.homebean.HomeSpinCircleBean;
import com.unohacha.qqj.fzdr.bean.homebean.MoreDataBean;
import com.unohacha.qqj.fzdr.util.MyUtils;
import com.unohacha.qqj.fzdr.util.SaveImageUtils;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;

/**
 * 点击看大图
 */
public class LookBigPicActivity extends BaseActivity {

    @BindView(R.id.photo_view)
    PhotoView photo_view;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_back)
    ImageView common_back;
    @BindView(R.id.common_right_text)
    TextView common_right_text;
    @BindView(R.id.common_right_image)
    ImageView common_right_image;
    @BindView(R.id.cur_page_tv)
    TextView cur_page_tv;
    @BindView(R.id.view_pager_photo)
    ViewPager mViewPager;

    private MyImageAdapter viewPagerAdapter;
    private List<String> imgUrl = new ArrayList<>();//
    private FindListBean.DataEntity.DataTwo mFindDataTwo = new FindListBean.DataEntity.DataTwo();
    private HomeSpinCircleBean.DataBeanX.DataBean dataBean = new HomeSpinCircleBean.DataBeanX.DataBean();
    private MoreDataBean.DataBeanX.DataBean mMoreDataBean = new MoreDataBean.DataBeanX.DataBean();
    private HomeSellerAreaListBean.DataBeanX.DataBean mAreaBean = new HomeSellerAreaListBean.DataBeanX.DataBean();
    private List<String> mPicList = new ArrayList<>();
    private int currentPosition = 0;
    private String tagPosiImg = "";//当前图片的路径
    private boolean isSaveSuccess;
    private String type = "0";//1. 2. 3. HomeSellerAreaListBean.DataBeanX.DataBean 

    @Override
    protected int getLayoutId() {
        return R.layout.activity_look_big_pic;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        super.initView();
        if (null != getIntent()) {
            currentPosition = getIntent().getIntExtra("currentPosition",0);
            if (null != getIntent().getSerializableExtra("listData")) {
                type = getIntent().getStringExtra("type");
                if (!TextUtils.isEmpty(type) && type.equals("1")) {
                    dataBean = (HomeSpinCircleBean.DataBeanX.DataBean) getIntent().getSerializableExtra("listData");
                    mPicList.addAll(dataBean.getList_pic());
                } else if (!TextUtils.isEmpty(type) && type.equals("2")) {
                    mMoreDataBean = (MoreDataBean.DataBeanX.DataBean) getIntent().getSerializableExtra("listData");
                    mPicList.addAll(mMoreDataBean.getList_pic());
                } else if (!TextUtils.isEmpty(type) && type.equals("3")) {
                    mAreaBean = (HomeSellerAreaListBean.DataBeanX.DataBean) getIntent().getSerializableExtra("listData");
                    mPicList.addAll(mAreaBean.getList_pic_compress());
                } else if (!TextUtils.isEmpty(type) && type.equals("4")){
                    mPicList = (List<String>) getIntent().getSerializableExtra("listData");
                }else if (!TextUtils.isEmpty(type) && "5".equals(type)){
                    imgUrl = getIntent().getStringArrayListExtra("imgUrl");
                    mPicList.addAll(imgUrl);
                }  else{
                    mFindDataTwo = (FindListBean.DataEntity.DataTwo) getIntent().getSerializableExtra("listData");
                    mPicList.addAll(mFindDataTwo.getList_pic());
                }

                tagPosiImg = mPicList.get(0);
                viewPagerAdapter = new MyImageAdapter(mPicList, this);

                mViewPager.setAdapter(viewPagerAdapter);
                mViewPager.setCurrentItem(currentPosition, false);
                cur_page_tv.setText(currentPosition + 1 + "/" + mPicList.size());
                mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        super.onPageSelected(position);
                        currentPosition = position;
                        cur_page_tv.setText(currentPosition + 1 + "/" + mPicList.size());
                        tagPosiImg = mPicList.get(position);
                    }
                });
            }else {
                type = getIntent().getStringExtra("type");
                 if (!TextUtils.isEmpty(type) && "5".equals(type)){
                    imgUrl = getIntent().getStringArrayListExtra("imgUrl");
                    mPicList.addAll(imgUrl);
                     tagPosiImg = mPicList.get(0);
                     viewPagerAdapter = new MyImageAdapter(mPicList, this);

                     mViewPager.setAdapter(viewPagerAdapter);
                     mViewPager.setCurrentItem(currentPosition, false);
                     cur_page_tv.setText(currentPosition + 1 + "/" + mPicList.size());
                     mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                         @Override
                         public void onPageSelected(int position) {
                             super.onPageSelected(position);
                             currentPosition = position;
                             cur_page_tv.setText(currentPosition + 1 + "/" + mPicList.size());
                             tagPosiImg = mPicList.get(position);
                         }
                     });
                }
            }
        }

        common_right_image.setVisibility(View.GONE);
        common_right_text.setVisibility(View.VISIBLE);
        common_right_text.setText("保存");
        common_right_text.setTextColor(getResources().getColor(R.color.black));
        commonTitle.setText("查看大图");

    }

    private Bitmap bitmap;

    @OnClick({R.id.common_back, R.id.common_right_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.common_back://返回按钮
                finish();
                break;
            case R.id.common_right_text://保存
                if (!TextUtils.isEmpty(tagPosiImg)) {
                    showLoading("保存中...");
                    Log.e("TAG:tagPosiImg",tagPosiImg);
                    new Thread() {
                        @Override
                        public void run() {
//                            try {
//                                bitmap = Glide.with(LookBigPicActivity.this)
//                                        .load(tagPosiImg)
//                                        .asBitmap()
//                                        .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
//
////                                isSaveSuccess = SaveImageUtils.saveImageToGallery(LookBigPicActivity.this, bitmap);
//                                mHandler.sendEmptyMessage(101);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            } catch (ExecutionException e) {
//                                e.printStackTrace();
//                            }
                            SaveImageUtils.saveImgFunc(tagPosiImg,LookBigPicActivity.this);
                            mHandler.sendEmptyMessage(101);
                        }
                    }.start();
                }
                break;
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 101:
                    hideLoading();
                    ToastUtils.showT("保存成功");
//                    SaveImageUtils.saveBmp2Gallery(bitmap, LookBigPicActivity.this);
//                    if (isSaveSuccess){
//                        ToastUtils.showToast(LookBigPicActivity.this,"保存成功");
//                    }else {
//                        ToastUtils.showToast(LookBigPicActivity.this,"保存失败");
//                    }
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mHandler) {
            mHandler = null;
        }
    }
}
